package com.bds.tjj.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bds.tjj.model.ConfidenData;
import com.bds.tjj.model.common.Result;
import com.bds.tjj.service.ConfidenSumService;
import com.bds.tjj.utils.Const;
import com.bds.tjj.utils.HttpUrlUtils;

/**
 * Created by guoyu on 2018/1/29.
 */
@RestController
public class ConfidenSumController extends BaseAction {
	private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
			.getLogger(ConfidenSumController.class);

	@Autowired
	ConfidenSumService confidenSumService;

	@RequestMapping(value = "/service/data/get_confiden_num/**", method = RequestMethod.GET)
	public Result getConfidenNum(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
		Result ret = new Result();
		String type = HttpUrlUtils.extractPathFromPattern(httpRequest);

		ConfidenData confidenData = null;
		try {
			confidenData = confidenSumService.getConfidenMonthNum(type);
			ret.setResult(Const.SUC, confidenData);
		} catch (Exception e) {
			ret.setResult(Const.FAIL, confidenData);
			LOGGER.warn("查询confiden_month_num异常：", e);
		}

		return ret;
	}

	/**
	 * 例子：/service/data/get_confiden_trend/index3/200709-200806
	 */
	@RequestMapping(value = "/service/data/get_confiden_trend/{prop}/{time}", method = RequestMethod.GET)
	public Result getConfidenTrend(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
			@PathVariable String prop, @PathVariable String time) throws IOException {
		Result ret = new Result();

		int startTime, endTime;
		try {
			startTime = Integer.parseInt(time.split("-")[0].substring(0, 6));
			endTime = Integer.parseInt(time.split("-")[1].substring(0, 6));
		} catch (Exception e) {
			ret.setResult(Const.FAIL, "参数异常");
			LOGGER.warn("get_confiden_trend 参数异常：", e);
			return ret;
		}

		try {
			List<ConfidenData> list = confidenSumService.getConfidenTrend(prop, startTime, endTime);
			ret.setResult(Const.SUC, list);
		} catch (Exception e) {
			ret.setResult(Const.FAIL, null);
			LOGGER.warn("查询get_confiden_trend异常：", e);
		}

		return ret;
	}
}
